package com.wtwd.wtwddevice.service.impl;

import com.wtwd.common.autonavi.service.AutonaviService;
import com.wtwd.common.netty.entity.Location;
import com.wtwd.common.netty.entity.vo.ResultCode;
import com.wtwd.common.netty.entity.vo.ResultVO;
import com.wtwd.common.netty.service.CoordinateTransformationService;
import com.wtwd.device.constant.LocationTypeEnum;
import com.wtwd.wtwddevice.model.entity.LocationData;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.ILocationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LocationServiceImpl implements ILocationService {

    @Resource
    private AutonaviService autonaviService;


    @Resource
    private IDeviceService deviceService;
    
    @Value("${gaoDe.key}")
    private String key;

    @Resource
    private CoordinateTransformationService transformationService;


    @Override
    public LocationData getLocationByLbsOrWifi(String imei, List<String> lbs, List<String> wifis, String network, boolean iscdma) {
        ResultVO resultVO = autonaviService.getLocationByLbsOrWifi(key, imei, lbs, wifis, network, iscdma);
        if (resultVO.getCode().equals(ResultCode.SUCCESS.getCode())){
            Location location = (Location)resultVO.getData();
            LocationData data = new LocationData();
            double[] doubles = transformationService.gcj02_To_Gps84(Double.valueOf(location.getLat()), Double.valueOf(location.getLng()));
            data.setLng(String.valueOf(doubles[1]));
            data.setLat(String.valueOf(doubles[0]));
            data.setLocationType(wifis.size()>2?LocationTypeEnum.WIFI.getType():LocationTypeEnum.LPS.getType());
            Long deviceId = deviceService.getDeviceIdByImei(imei);
            data.setDeviceId(deviceId);
            return data;
        }
        return null;
    }


}
